<!doctype html>
<html lang="en">
<head>
    <title>Tree - Object Model</title>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="description" content="Object model" />
    <meta name="keywords" content="" />
    <meta name="author" content="ComponentOne" />
    
    <link href="../../themes/rocket/jquery-wijmo.css" rel="stylesheet" type="text/css" />
    
    <link href="../../themes/wijmo/jquery.wijmo-open.2.3.2.css" rel="stylesheet" type="text/css" />
    <link href="../../themes/wijmo/jquery.wijmo.wijtree.css" rel="stylesheet" type="text/css" />
    <script src="../../external/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="../../external/jquery-ui-1.9.1.custom.min.js" type="text/javascript"></script>
    <script src="../../external/jquery.wijmo-open.all.2.3.2.min.js" type="text/javascript"></script>
    <script src="../../Wijmo/jquery.wijmo.wijtree.js" type="text/javascript"></script>
    <script id="scriptInit" type="text/javascript">

        var netflx = "http://odata.netflix.com/Catalog/Genres('{category}')/Titles?$inlinecount=allpages&$callback=callback&$top=10&$format=json&$orderby=AverageRating desc";
        $(document).ready(function () {
            var categories = ["Chinese Movies", "Thai Movies", "Korean Movies", "Japanese Movies"]

            $("#tree").wijtree({
                nodeExpanding: function (events, data) {
                    var url = netflx.replace(/\{category\}/, data.params),
                    node = data.node,
                    o = node.options;

                    if (o.hasChildren && node.element.find("li:wijmo-wijtreenode").length == 0) {
                        $.ajax({
                            dataType: "jsonp",
                            url: url,
                            jsonpCallback: "callback",
                            success: function (result) {
                                var addedNode = {};
                                var movies = result["d"]["results"];

                                for (var i = 0; i < movies.length; i++) {

                                    addedNode.text = movies[i].Name;
                                    addedNode.url = "#"
                                    data.node.add(addedNode);
                                }

                            }
                        });
                    }                   
                }
            });

            for (var i = 0; i < categories.length; i++) {
                $("#tree").wijtree("add", {
                    text: categories[i],
                    hasChildren: true,
                    url: "#",
                    params: categories[i]
                });
            }
        });

    </script>
    <style type="text/css">
    .wijmo-wijtree
    {
         width:500px;    
    }
    </style>
</head>
<body class="demo-single">
    <div class="container">
        <div class="header">
            <h2>
                Load Nodes with Ajax</h2>
        </div>
        <div class="main demo">
            <!-- Begin demo markup -->
            <div>
                <ul id="tree">                   
                </ul>
            </div>
            <!-- End demo markup -->
            <div class="demo-options">
                <!-- Begin options markup -->
              
                <!-- End options markup -->
            </div>
        </div>
        <div class="footer demo-description">
		    <p>
		        This sample shows how to dynamically add and remove nodes from the wijtree widget.  
		    </p>
        </div>
    </div>
</body>
</html>
